package com.niit.onlinemusicserver.algorithm;
public class Similarity {
    /** 计算距离：欧式距离 */
    public static float calculateSimilarity(float[] curRating, float[] otherRating) {
        float similarity = 0f;
        int len = curRating.length;
        int cnt = 0;
        for (int i = 0; i < len; i++) {
            //根据被当前用户“评分”了的项目来计算相似度，由于浮点数不能==比较，这里用0.01f表示0.0f
            if (curRating[i] > 0.01f) {
                similarity += Math.pow(curRating[i] - otherRating[i], 2);
                cnt++;
            }
        }
        similarity /= (cnt > 0 ? cnt : 1);

        return similarity;
    }
}
